package com.me.forum.controller;

import com.me.enums.RespType;
import com.me.forum.domain.User;
import com.me.forum.request.UserRequest;
import com.me.forum.service.IUserService;
import com.me.forum.utils.components.model.IModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired
	private IUserService userService;

	@GetMapping("/{id}")
	public IModel getById(@PathVariable String id, IModel model) {
		User u = userService.findById(id);
		model.success(u != null);
		model.add(RespType.User, u);
		return model;
	}

	/**
	 * @description:用户注册功能
	 * @author:LWP
	 * @createTime:2020-04-26 19:38
	 */
	@PostMapping("/register")
	public Map<String, Object> register(@RequestBody UserRequest ureq, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		User u = userService.register(ureq);
		session.setAttribute("user",u);
		map.put("success", u != null);
		map.put("user", u);
		return map;
	}

	/**
	 * @description:用户登录功能
	 * @author:ly
	 * @createTime:2020-04-28 19:40
	 */
	// @PostMapping(value = "/login")
	// public Object login(String username, String password,HttpSession session) {
	// 	Map<String, Object> map = new HashMap<>();
	// 	User u = userService.login(username, password);
	// 	session.setAttribute("user",u);
	// 	map.put("success", u != null);
	// 	map.put("user", u);
	// 	return map;
	// }
	
	@GetMapping("/login")
	public IModel login(HttpSession session, IModel model) {
		model.add(RespType.User, session.getAttribute("user"));
		return model;
	}
}
